layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;

    //验证码
    var verification_code = [];
    draw(verification_code);
    $("#canvas").on('click',function(){
        draw(verification_code);
    });

    // 登录过期的时候，跳出ifram框架
    if (top.location != self.location) {
        top.location = self.location;
    }

    // //粒子线条背景
    // $(document).ready(function(){
    //     $('.layui-container').particleground({
    //         dotColor:'#5cbdaa',
    //         lineColor:'#5cbdaa'
    //     });
    // });

    $(".loginBody .seraph").click(function(){
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
            time:5000
        });
    })

    //登录按钮
    form.on("submit(login)",function(data){
        // var data=JSON.stringify(data);
        // console.log("****************"+data);
        data=data.field;
        if (data.username == '') {
            layer.msg('用户名不能为空');
            return false;
        }
        if (data.password == '') {
            layer.msg('密码不能为空');
            return false;
        }
        var verificationCode = verification_code.join("");
        if (data.captcha == '') {
            layer.msg('验证码不能为空');
            return false;
        } else if(data.captcha.toUpperCase() != verificationCode) {
            layer.msg('验证码输入错误');
            $("#canvas").click()
            return false;
        }

        $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        var param = {"phone" : $("#phone").val(), "lpass" : $("#lpass").val()};
        $.ajax({
            url:"login.do",
            type:"POST",
            data:data,
            dataType:"json",
            success:function (res) {
                console.log("&&&&&&&&&&&&&&&"+res);
                if(res.code==4001){
                    window.location="/index.do"
                }else{
                    layer.msg(res.msg,{"time":1000},function(){
                        parent.location.reload();
                    });
                }
            }
        });
        return false;
    })

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })

    function draw(verification_code) {
        var canvas_width = $('#canvas').width();
        var canvas_height = $('#canvas').height();
        var canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");
        canvas.width = canvas_width;
        canvas.height = canvas_height;

        var randomCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
        var randomCodeStr = randomCode.split(",");
        var randomCodeLength = randomCodeStr.length;
        for (var i = 0; i <= 3; i++) { //绘制数字
            var j = Math.floor(Math.random() * randomCodeLength);
            var rotate = Math.random() * 50 * Math.PI / 180;
            var content = randomCodeStr[j];
            verification_code[i] = content.toUpperCase();
            var x = 10 + i * 20;
            var y = 20 + Math.random() * 8;
            context.font = "bold 23px 微软雅黑";
            context.translate(x, y);
            context.rotate(rotate);
            context.fillStyle = randomColor();
            context.fillText(content, 0, 0);
            context.rotate(-rotate);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //绘制线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 60; i++) { //绘制小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }

    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
})
